﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DAL;

public partial class admin_Students_ListClass : System.Web.UI.Page
{

    ClassBLL data = new ClassBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["url_back_admin"] = Request.RawUrl;

            LoadCategory();
            ShowData();
        }
    }

    private void ShowChild(int parentId, string space)
    {
        List<Category> listDataSource = data.GetByParent(parentId,true);

        foreach (Category item in listDataSource)
        {
            ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
            dropParent.Items.Add(obj);
            ShowChild(item.Id, space + "- - - ");
        }
    }

    private void LoadCategory()
    {
        dropParent.Items.Clear();
        ShowChild(14, " - - ");
        ListItem itemRoot = new ListItem("Danh sách lớp học", "0");
        dropParent.Items.Insert(0, itemRoot);

    }

    private void ShowData()
    {
        int parentId = Convert.ToInt32(dropParent.SelectedValue);

        if (dropIsEnable.SelectedIndex == 0)
        {
            if (parentId != 0)
                gridView1.DataSource = data.GetByParentClass(parentId);
            else
                gridView1.DataSource = data.GetAll();
        }
        else
        {
            bool isEnable = true;
            if (dropIsEnable.SelectedValue == "2") isEnable = false;

            if (parentId != -1)
                gridView1.DataSource = data.GetByParentClass(parentId, isEnable);
            else
                gridView1.DataSource = data.GetAll(isEnable);
        }
        gridView1.DataBind();

        //Bo mui ten len xuong dau va cuoi
        //if (gridView.Rows.Count > 0)
        //{
        //    gridView.Rows[0].Cells[6].Enabled = false;
        //    gridView.Rows[gridView.Rows.Count - 1].Cells[5].Enabled = false;
        //}
    }

    private void Delete_Dequy(int id)
    {
         data.Delete(id);
    }

    private void Enable(bool isEnable)
    {
        foreach (GridViewRow item in gridView1.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    if (check.Checked)
                    {
                        int id = Convert.ToInt32(item.Cells[2].Text);
                        data.Enable(id, isEnable);
                    }
                    break;
                }
            }
        }
        ShowData();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowIndex >= 0) //Bo qua dong dau
            {
                int id = Convert.ToInt32(e.Row.Cells[2].Text);
                if (e.Row.Cells[4].Text == "0") e.Row.Cells[4].Text = "Danh mục gốc";
                else e.Row.Cells[4].Text = data.GetByParentClassId(Convert.ToInt32(e.Row.Cells[4].Text), true).Name;

                bool enables = Convert.ToBoolean(data.GetById(id).isEnable);
                //Icon check
                Image img = new Image();
                foreach (Control item in e.Row.Cells[5].Controls)
                {
                    if (item.GetType() == typeof(Image))
                    {
                        img = (Image)item;
                        break;
                    }
                }
                if (enables)
                    img.ImageUrl = "~/Images/check.png";
                else
                {
                    img.ImageUrl = "~/Images/an.png";

                }

            }
        }
        catch { }
    }
    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        lbThongBao.Text = "";
        try
        {
            gridView1.PageIndex = e.NewPageIndex;
            ShowData();
        }
        catch { }
    }


    protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            int id = Convert.ToInt32(gridView1.Rows[e.RowIndex].Cells[2].Text);
            //delete image
            Delete_Dequy(id);
            Myfunction.ThongBao(lbThongBao, "Danh mục đã được xóa");

            LoadCategory();
            ShowData();
        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/Students/DetailClass.aspx");
    }
    protected void btnVisible_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(false);
    }
    protected void btnEnable_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(true);
    }
    protected void btnXoaChon_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView1.Rows)
            {
                foreach (Control ctrl in item.Cells[1].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        if (check.Checked)
                        {
                            int id = Convert.ToInt32(item.Cells[2].Text);
                            Delete_Dequy(id);
                        }
                        break;
                    }
                }
            }
            LoadCategory();
            ShowData();
        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }

    protected void linkCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView1.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = true;
                    break;
                }
            }
        }
    }
    protected void linkUnCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView1.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = false;
                    break;
                }
            }
        }
    }
    protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
    protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        gridView1.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
        ShowData();
    }
    protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
}